% ch7.tex
% This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 New Zealand License.
% To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/nz
% or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


\chapter{A short chapter about Files}\label{ch:ashortchapteraboutfiles}\index{functions!file}

You probably know what a file is already.
\par
\noindent
If your parents have a home office, chances are they've got a file cabinet of some sort.  Various important papers (mostly boring adult stuff) are stored in those cabinets, usually in cardboard folders labelled with letters of the alphabet, or months of the year. Files on a computer are rather similar to those cardboard folders. They have labels (the name of the file), and are used to store important information. The drawers on a file cabinet, which might be used to organise paperwork so they are easier to find, are similar to directories (or folders) on a computer.
\par
We've already created a file object, using Python, in the previous chapter.  The example looked like this:

\begin{WINDOWS}

\begin{listing}
\begin{verbatim}
>>> f = open('c:\\test.txt')
>>> print(f.read())
\end{verbatim}
\end{listing}

\end{WINDOWS}

\begin{MAC}

\begin{listing}
\begin{verbatim}
>>> f = open('Desktop/test.txt')
>>> print(f.read())
\end{verbatim}
\end{listing}

\end{MAC}

\begin{LINUX}

\begin{listing}
\begin{verbatim}
>>> f = open('Desktop/test.txt')
>>> print(f.read())
\end{verbatim}
\end{listing}
 
\end{LINUX}

A file object doesn't just have the function \code{read}\index{functions!file!read}. After all, file cabinets wouldn't be very useful if you could only open a drawer and take papers out, but could never put them back in. We can create a new, empty file, by passing another parameter when we call the \code{file} function:

\begin{listing}
\begin{verbatim}
>>> f = open('myfile.txt', 'w')
\end{verbatim}
\end{listing}

'w' is the way we tell Python we want to write to the file object, and not read from it.  We can now add information to the file using the function \code{write}\index{functions!file!write}.

\begin{listing}
\begin{verbatim}
>>> f = open('myfile.txt', 'w')
>>> f.write('this is a test file')
\end{verbatim}
\end{listing}

Then we need to tell Python when we're finished with the file, and don't want to write to it any more---we use the function \code{close}\index{functions!file!close} to do this.

\begin{listing}
\begin{verbatim}
>>> f = open('myfile.txt', 'w')
>>> f.write('this is a test file')
>>> f.close()
\end{verbatim}
\end{listing}

If you open the file using your favourite editor, you will see it contains the text: ``this is a test file''.  Or better yet, we can use Python to read it in again:

\begin{listing}
\begin{verbatim}
>>> f = open('myfile.txt')
>>> print(f.read())
this is a test file
\end{verbatim}
\end{listing}

\newpage